package com.dfiov.cloud.msmtools.service;

import com.dfiov.cloud.msmtools.enums.ResponseEnum;
import com.dfiov.cloud.msmtools.service.feign.MsAccountLoginAuthFeign;
import com.dfiov.cloud.msmtools.utils.MD5Utils;
import com.dfiov.cloud.msmtools.vo.AccountLoginAuthVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *@Title 重置密码实现类
 *
 *@author xuyz
 *@create 2025/2/20 19:17
 *@description
 */

@Service("accountLoginAuthService")
public class AccountLoginAuthService {
    private static final Logger log = LoggerFactory.getLogger(AccountLoginAuthService.class);

    @Autowired
    private MsAccountLoginAuthFeign msAccountLoginAuthFeign;

    /**
     * 根据sid获取信息
     */
    public AccountLoginAuthVo getBySid(Long sid) {
        return msAccountLoginAuthFeign.findBySid(sid);
    }

    /**
     * 根据登录类型和登录ID获取信息
     * @param loginType
     * @param loginId
     * @param projectId : pateo  dfpvivi  dnaivi  mivi
     * @return
     */
    public AccountLoginAuthVo findByLoginTypeAndLoginId(Short loginType, String loginId, String projectId) {
        return msAccountLoginAuthFeign.findByLoginTypeAndLoginId(loginType, loginId, projectId);
    }


    /**
     * 更新登录账号信息.
     *
     * @param loginType
     *            登录类型
     * @param loginId
     *            登录ID
     * @param projectId
     *            项目ID
     * @param updatePassWord
     *            是否更新密码
     * @return boolean
     */
    public boolean update( String loginId, Short loginType,String projectId, boolean updatePassWord) throws Exception {
        AccountLoginAuthVo accountLoginAuthVo = findByLoginTypeAndLoginId(loginType, loginId, projectId);
        // 参数校验
        checkParams(accountLoginAuthVo);

        if (updatePassWord) {
            accountLoginAuthVo.setPassword(MD5Utils.getMD5(accountLoginAuthVo.getPassword()));
        }
        accountLoginAuthVo.setUpdateDate(new Date());
        AccountLoginAuthVo isOk = msAccountLoginAuthFeign.edit(accountLoginAuthVo.getSid(), accountLoginAuthVo);
        return isOk != null;
    }

    public boolean update( String loginId, Short loginType,String projectId) throws Exception {
        return update(loginId,loginType, projectId, true);
    }

    /**
     * 更新登录账号信息（默认）.
     * @param loginType
     *            登录类型
     * @param loginId
     *            登录ID
     * @param projectId
     *            项目ID
     * @param updatePassWord
     *            是否更新密码
     * @return boolean
     */

    public boolean updateDefault(String loginId, Short loginType,String projectId, boolean updatePassWord) throws Exception {
        AccountLoginAuthVo accountLoginAuthVo = findByLoginTypeAndLoginId(loginType, loginId, projectId);
        // 参数校验
        checkParams(accountLoginAuthVo);

        if (updatePassWord) {
            accountLoginAuthVo.setPassword(ResponseEnum.DEFAULT_RESET_PASSWORD.getCode());
        }
        accountLoginAuthVo.setUpdateDate(new Date());
        AccountLoginAuthVo isOk = msAccountLoginAuthFeign.edit(accountLoginAuthVo.getSid(), accountLoginAuthVo);
        return isOk != null;
    }

    public boolean updateDefault(String loginId, Short loginType,String projectId) throws Exception {
        return updateDefault(loginId,loginType, projectId, true);
    }

    /*
     * 参数校验
     * UserAccountSId 不能为空
     * LoginId 不能为空
     * Password 不能为空
     * sid 不能为空
     */
    public void checkParams(AccountLoginAuthVo accountLoginAuthVo) throws Exception {
        if (accountLoginAuthVo.getUserAccountSid() == null || accountLoginAuthVo.getUserAccountSid() == 0) {
            log.error("{},{},UserAccountSid is empty", ResponseEnum.INPUT_PARAMS_ERROR.getCode(), ResponseEnum.INPUT_PARAMS_ERROR.getMsg());
            throw new Exception(ResponseEnum.INPUT_PARAMS_ERROR.getCode() + ":" + ResponseEnum.INPUT_PARAMS_ERROR.getMsg());
        }

        if ("".equals(accountLoginAuthVo.getLoginId())) {
            log.error("{},{},LoginId is empty", ResponseEnum.INPUT_PARAMS_ERROR.getCode(), ResponseEnum.INPUT_PARAMS_ERROR.getMsg());
            throw new Exception(ResponseEnum.INPUT_PARAMS_ERROR.getCode() + ":" + ResponseEnum.INPUT_PARAMS_ERROR.getMsg());
        }

        if ("".equals(accountLoginAuthVo.getPassword())) {
            log.error("{},{},password is empty", ResponseEnum.INPUT_PARAMS_ERROR.getCode(), ResponseEnum.INPUT_PARAMS_ERROR.getMsg());
            throw new Exception(ResponseEnum.INPUT_PARAMS_ERROR.getCode() + ":" + ResponseEnum.INPUT_PARAMS_ERROR.getMsg());
        }

        AccountLoginAuthVo accountLoginAuthVoBySid = this.getBySid(accountLoginAuthVo.getSid());
        if (accountLoginAuthVoBySid == null || !accountLoginAuthVoBySid.getUserAccountSid().equals(accountLoginAuthVo.getUserAccountSid())) {
            log.error("{},{},UserAccountSid is error", ResponseEnum.ACCOUNT_LOGIN_AUTH_NOT_EXIST_ERROR.getCode(), ResponseEnum.ACCOUNT_LOGIN_AUTH_NOT_EXIST_ERROR.getMsg());
            throw new Exception(ResponseEnum.ACCOUNT_LOGIN_AUTH_NOT_EXIST_ERROR.getCode() + ":" + ResponseEnum.ACCOUNT_LOGIN_AUTH_NOT_EXIST_ERROR.getMsg());
        }
    }
}
